package com.store.app.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.google.gson.Gson;
import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.MainActivity;
import com.store.app.R;
import com.store.app.adapter.GoodsAdapter;
import com.store.app.bean.EventBusBean;
import com.store.app.bean.GoodsBean;
import com.store.app.bean.GoodsListNewBean;
import com.store.app.http.Stores;
import com.store.app.http.Submit;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.GoToLogin;
import com.store.app.utils.ToastUtils;
import com.store.app.utils.UserTokenUtils;
import com.store.app.utils.Utils;
import com.store.app.widget.AutoListView;
import com.store.app.widget.AutoListView.OnLoadListener;
import com.store.app.widget.AutoListView.OnRefreshListener;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * 商品
 */
public class GoodsActivity extends BaseActivity implements OnClickListener, IOnHttpListener {
    private ImageView ivScan;
    private ImageView ivAdd;
    private EditText etSearch;
    private AutoListView lv;
    private List<GoodsListNewBean> list;
    private GoodsAdapter mAdapter;

    private ImageView img_fail_nonet;
    private ImageView img_fail_notext;
    private static final int MSG_LOAD_SUCCESS = 0;
    private static final int MSG_LOAD_FAILED = 1;

    private static final int MSG_REFRESH_SUCCESS = 2;
    private static final int MSG_REFRESH_FAILED = 3;
    private static final int MSG_TOKEN = 99;

    private static final int MSG_SEARCH_SUCCESS = 4;
    private static final int MSG_SEARCH_FAILED = 5;

    private static final int MSG_DELETE_SUCCESS = 6;
    private static final int MSG_DELETE_FAILED = 7;
    private static final int MSG_LOAD_NOTNET = 8;
    private static final int REQUEST_ADD = 1;
    private static final int REQUEST_SCAN = 2;
    private static final int REQUEST_EDIT = 3;

    private int pageNum;
    private int pageSize;

    private Handler mHandler;
    private RelativeLayout loadfail;
    private LinearLayout lin_loadNull;//加载为空
    private LinearLayout lin_loadfail;  //无网络页面
    private TextView tvReload;
    private GoToLogin goToLogin;
    private NetRequest netRequest;
    private TextView tv_all_goods;
    private LinearLayout ll_all_goods;      //全部
    private TextView tv_cuxiao_goods;
    private LinearLayout ll_cuxiao_goods;       //促销
    private TextView tv_wangdian_goods;
    private LinearLayout ll_wangdian_goods;     //网店
    private TextView tv_quick_goods;
    private LinearLayout ll_quick_goods;        //快选
    private TextView tv_data_source_goods;
    private TextView tv_sjx;
    private RelativeLayout rl_sjx;
    private LinearLayout ll_data_source_goods;  //来源
    private int currentShows = 1;//1 全部  2 促销  3网店 4 快选 5 数据源
    private int currentData = 1;//1 我要批 2 惠易定 3 自定义 4 领了么==>淘淘领 5  伙拼团==>拼团领
    private TextView tv_wyp;
    private TextView tv_hyd;
    private TextView tv_llm;
    private TextView tv_hpt;
    private TextView tv_zdy;
    private PopupWindow popupWindow;
    private ImageView public_ll_return;

    @Override
    public void onAfinalFail() {
        if(lv!=null){
            lv.onLoadComplete();
            lv.onRefreshComplete();
        }
        dismissProgressDialog();
        showToast("请检查本地网络！");
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg2) {
        dismissProgressDialog();
        if (requestId == 1) {
            try {
                JSONObject data = new JSONObject(result);
                JSONArray listArr = data.getJSONArray("list");
                Gson gson = new Gson();
                List<GoodsListNewBean> goodsBeanList = new ArrayList<>();
                for (int i = 0; i < listArr.length(); i++) {
                    GoodsListNewBean goodsBean = gson.fromJson(listArr.getJSONObject(i).toString(), GoodsListNewBean.class);
                    goodsBeanList.add(goodsBean);
                }
                Message msg = Message.obtain();
                msg.what = MSG_LOAD_SUCCESS;
                msg.arg1 = data.getJSONObject("page").getInt("page_no");
                msg.obj = goodsBeanList;
                mHandler.sendMessage(msg);
            } catch (JSONException e) {
            }
        } else if (requestId == 2) {
            try {
                JSONObject data = new JSONObject(result);
                JSONArray listArr = data.getJSONArray("list");
                Gson gson = new Gson();
                List<GoodsListNewBean> goodsBeanList = new ArrayList<>();
                for (int i = 0; i < listArr.length(); i++) {
                    GoodsListNewBean goodsBean = gson.fromJson(listArr.getJSONObject(i).toString(), GoodsListNewBean.class);
                    goodsBeanList.add(goodsBean);
                }
                Message msg = Message.obtain();
                msg.what = MSG_REFRESH_SUCCESS;
                msg.arg1 = data.getJSONObject("page").getInt("page_no");
                msg.obj = goodsBeanList;
                mHandler.sendMessage(msg);
            } catch (JSONException e) {
            }
        }else if(requestId == 3){
            try {
                JSONObject data = new JSONObject(result);
                Gson gson = new Gson();
                JSONArray listArr = data.getJSONArray("list");
                List<GoodsListNewBean> goodsBeanList = new ArrayList<>();
                for (int i = 0; i < listArr.length(); i++) {
                    GoodsListNewBean goodsBean = gson.fromJson(listArr.getJSONObject(i).toString(), GoodsListNewBean.class);
                    goodsBeanList.add(goodsBean);
                }


                Message msg = Message.obtain();
                msg.what = MSG_REFRESH_SUCCESS;
                msg.obj = goodsBeanList;
                mHandler.sendMessage(msg);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    private void initPop() {
        View popView = LayoutInflater.from(this).inflate(R.layout.pop_data_source, null);
        tv_wyp = (TextView) popView.findViewById(R.id.tv_wyp);
        tv_hyd = (TextView) popView.findViewById(R.id.tv_hyd);
        tv_llm = (TextView) popView.findViewById(R.id.tv_llm);
        tv_hpt = (TextView) popView.findViewById(R.id.tv_hpt);
        tv_zdy = (TextView) popView.findViewById(R.id.tv_zdy);
        popupWindow = new PopupWindow(popView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        tv_wyp.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                currentData = 1;
                currentShows = 5;
                refreshData();
                if (popupWindow != null) {
                    popupWindow.dismiss();
                }
                refreshPopUI();
            }
        });
        tv_hyd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                currentData = 2;
                currentShows = 5;
                refreshData();
                if (popupWindow != null) {
                    popupWindow.dismiss();
                }
                refreshPopUI();
            }
        });
        tv_llm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                currentData = 4;
                currentShows = 5;
                refreshData();
                if (popupWindow != null) {
                    popupWindow.dismiss();
                }
                refreshPopUI();
            }
        });
        tv_hpt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                currentData = 5;
                currentShows = 5;
                refreshData();
                if (popupWindow != null) {
                    popupWindow.dismiss();
                }
                refreshPopUI();
            }
        });
        tv_zdy.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                currentData = 3;
                currentShows = 5;
                refreshData();
                if (popupWindow != null) {
                    popupWindow.dismiss();
                }
                refreshPopUI();
            }
        });

    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        dismissProgressDialog();
        if (requestId == 1) {
            ToastUtils.showTextToast(this, msg);
            Message msg2 = Message.obtain();
            msg2.what = MSG_LOAD_FAILED;
            mHandler.sendMessage(msg2);
        } else if (requestId == 2) {
            ToastUtils.showTextToast(this, msg);
            Message msg2 = Message.obtain();
            msg2.what = MSG_REFRESH_FAILED;
            mHandler.sendMessage(msg2);
        }else if(requestId ==3 ){
            ToastUtils.showTextToast(this, msg);
            Message msg3 = Message.obtain();
            msg3.what = MSG_SEARCH_FAILED;
            mHandler.sendMessage(msg3);
        }
    }

    public void showLoadResult(int state) {
        //无网络情况
        if (state == 1) {
            loadfail.setVisibility(View.VISIBLE);
            lin_loadfail.setVisibility(View.VISIBLE);
            lin_loadNull.setVisibility(View.GONE);
        }//无数据情况
        else if (state == 2) {
            loadfail.setVisibility(View.VISIBLE);
            lin_loadfail.setVisibility(View.GONE);
            lin_loadNull.setVisibility(View.VISIBLE);
        }//都不显示
        else {
            loadfail.setVisibility(View.GONE);
        }
    }

    private void initView() {
        tv_all_goods = (TextView) findViewById(R.id.tv_all_goods);
        ll_all_goods = (LinearLayout) findViewById(R.id.ll_all_goods);
        tv_cuxiao_goods = (TextView) findViewById(R.id.tv_cuxiao_goods);
        ll_cuxiao_goods = (LinearLayout) findViewById(R.id.ll_cuxiao_goods);
        tv_wangdian_goods = (TextView) findViewById(R.id.tv_wangdian_goods);
        ll_wangdian_goods = (LinearLayout) findViewById(R.id.ll_wangdian_goods);
        tv_quick_goods = (TextView) findViewById(R.id.tv_quick_goods);
        ll_quick_goods = (LinearLayout) findViewById(R.id.ll_quick_goods);
        tv_data_source_goods = (TextView) findViewById(R.id.tv_data_source_goods);
        ll_data_source_goods = (LinearLayout) findViewById(R.id.ll_data_source_goods);
        tv_sjx = (TextView) findViewById(R.id.tv_sjx);
        rl_sjx = (RelativeLayout) findViewById(R.id.rl_sjx);
        rl_sjx.setOnClickListener(this);
        ll_all_goods.setOnClickListener(this);
        ll_cuxiao_goods.setOnClickListener(this);
        ll_wangdian_goods.setOnClickListener(this);
        ll_quick_goods.setOnClickListener(this);
        ll_data_source_goods.setOnClickListener(this);


        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        public_ll_return.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private class WeakHandler extends Handler {
        private final WeakReference<GoodsActivity> mReference;

        public WeakHandler(GoodsActivity activity) {
            mReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            final GoodsActivity activity = mReference.get();

            if (activity == null) {
                return;
            }
            activity.dismissProgressDialog();
            switch (msg.what) {
                case MSG_SEARCH_SUCCESS:
                    activity.lv.onLoadComplete();
                    GoodsListNewBean resultSearch = (GoodsListNewBean) msg.obj;
                    if (!TextUtils.isEmpty(resultSearch.getBarcode())) {
                        activity.lv.setResultSize(0);
                        activity.list.clear();
                        activity.list.add(resultSearch);
                        activity.mAdapter.notifyDataSetChanged();
                        activity.lv.setResultSize(0);
                        activity.lv.onLoadComplete();
                    } else {
                        activity.lv.setResultSize(0);
                        activity.list.clear();
                        activity.mAdapter.notifyDataSetChanged();
                        activity.lv.setResultSize(0);
                        activity.lv.onLoadComplete();
                    }

                    break;
                case MSG_SEARCH_FAILED:
                    activity.lv.setResultSize(0);
                    activity.list.clear();
                    activity.mAdapter.notifyDataSetChanged();
                    activity.lv.setResultSize(0);
                    activity.lv.onLoadComplete();
//					activity.showToast((String) msg.obj);
                    showLoadResult(1);
                    break;
                case MSG_LOAD_SUCCESS:
                    activity.lv.onLoadComplete();
                    if (msg.arg1 == activity.pageNum) {
                        @SuppressWarnings("unchecked")
                        List<GoodsListNewBean> result = (List<GoodsListNewBean>) msg.obj;
                        activity.lv.setResultSize(result.size());
                        activity.list.addAll(result);
                        activity.mAdapter.notifyDataSetChanged();
                        activity.pageNum += 1;
                        if (activity.list.size() == 0) {
//							activity.lin_loadfail.setVisibility(View.VISIBLE);
//							activity.img_fail_nonet.setVisibility(View.GONE);
//							activity.img_fail_notext.setVisibility(View.VISIBLE);
//							activity.tv_fail.setText("暂无内容");
                            showLoadResult(2);
                        } else {
                            showLoadResult(3);
                        }
                    }
                    break;
                case MSG_LOAD_FAILED:
                    activity.lv.setResultSize(0);
                    activity.lv.onLoadComplete();
                    showLoadResult(1);
//					activity.showToast((String) msg.obj);
                    break;
                case MSG_REFRESH_SUCCESS:
                    Log.v("zyl", "刷新了图片列表");
                    activity.lv.onRefreshComplete();
                    @SuppressWarnings("unchecked")
                    List<GoodsListNewBean> result = (List<GoodsListNewBean>) msg.obj;
//                    for (int i = 0; i < result.size(); i++) {
//                        Log.v("zyl", "bean pic:" + result.get(i).getGoodsUrl());
//                    }
                    activity.lv.setResultSize(result.size());
                    activity.list.clear();
                    activity.list.addAll(result);
                    activity.mAdapter.notifyDataSetChanged();
                    activity.pageNum += 1;
                    if (activity.list.size() >= 0) {
//						activity.lin_loadfail.setVisibility(View.GONE);
                        showLoadResult(3);
                    }
                    break;
                case MSG_REFRESH_FAILED:
                    activity.lv.onRefreshComplete();
                    showLoadResult(1);

//					activity.showToast((String) msg.obj);
                    break;
                case MSG_DELETE_SUCCESS:
                    if (activity.list.size() > 1) {
                        activity.lv.setSelection(1);
                    }
                    activity.dismissProgressDialog();
                    activity.lv.onRefresh();
                    showLoadResult(3);
                    break;
                case MSG_DELETE_FAILED:
                    activity.dismissProgressDialog();
//					activity.showToast((String) msg.obj);
                    showLoadResult(1);
                    break;
                //无网络
                case MSG_LOAD_NOTNET:
                    showLoadResult(1);
//					activity.lin_loadfail.setVisibility(View.VISIBLE);
//					activity.lin_loadfail.setOnClickListener(new OnClickListener() {
//						@Override
//						public void onClick(View v) {
//							activity.lin_loadfail.setVisibility(View.GONE);
//							activity.lv.onLoad();
//						}
//					});

                case MSG_TOKEN:
//                    Toast.makeText(GoodsActivity.this, "您的账号已在其他手机登录，请重新登录！", Toast.LENGTH_LONG).show();

                    break;
                default:
                    break;
            }
        }
    }


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_goods);
        initView();
        ExitApplication.getInstance().addActivity(this);
        netRequest = new NetRequest(this);
//		getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        list = new ArrayList<>();
        mHandler = new WeakHandler(this);
        pageNum = 1;
        pageSize = 15;
        findViews();
        setListeners();
        lv.onLoad();
        goToLogin = new GoToLogin(this);
        initPop();
        refreshUI();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void findViews() {
        loadfail = (RelativeLayout) findViewById(R.id.loadfail);
        lin_loadfail = (LinearLayout) findViewById(R.id.ll_load_fail);//无网络
        lin_loadNull = (LinearLayout) findViewById(R.id.ll_load_null);//无网络
        tvReload = (TextView) findViewById(R.id.tv_reload);
        tvReload.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick()) {
                    return;
                }
                if (lv != null) {
                    list.clear();
                    mAdapter.notifyDataSetChanged();
                    pageNum = 1;
                    lv.onLoad();
                }
            }
        });

        img_fail_nonet = (ImageView) findViewById(R.id.loadfail_nonet);
        img_fail_notext = (ImageView) findViewById(R.id.loadfail_notext);
        ivScan = (ImageView) findViewById(R.id.iv_scan);
        ivAdd = (ImageView) findViewById(R.id.iv_add);
        etSearch = (EditText) findViewById(R.id.et_search);
        lv = (AutoListView) findViewById(R.id.lv_goods);
        lv.setDivider(null);
        mAdapter = new GoodsAdapter(list, GoodsActivity.this);
        lv.setAdapter(mAdapter);
        UserTokenUtils.setUerToken(this);
    }

    private void setListeners() {
        ivScan.setOnClickListener(this);
        ivAdd.setOnClickListener(this);

        etSearch.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //requestSearch();
                    showProgressDialog("", "正在搜索...");
//                    new requestSearch2().start();
                    queryGoodsData();
                }
                return false;
            }
        });

//        etSearch.addTextChangedListener(new TextWatcher() {
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                // TODO Auto-generated method stub
//
//            }
//
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                // TODO Auto-generated method stub
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                if (TextUtils.isEmpty(s.toString()) && pageNum == 1) {
//                    //requestStoreGoodsList();
////                    new requestStoreGoodsList2().start();
//                    netRequest.requestGoodsSearch(1, pageNum + "", pageSize + "");
//                }
//            }
//        });

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i("position", "position: " + position);
                if (position < 1 || position >= list.size() + 1) {
                    return;
                }
                Intent intent = new Intent(GoodsActivity.this, GoodDetailsNewActivity.class);
                intent.putExtra("item_store_id", list.get(position - 1).getStores_goods_id());
                startActivityForResult(intent, REQUEST_EDIT);
            }
        });

        lv.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                if (position < 1 || position >= list.size() + 1) {
                    return true;
                }
                showConfirmDialog("删除商品", "确定删除\"" + list.get(position - 1).getGoods_name() + "\"?", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //requestDelete(position - 2);
                        showProgressDialog("", "正在删除" + list.get(position - 1).getGoods_name());
                        new requestDelete2(position - 1).start();
                    }
                });

                return true;
            }
        });

        lv.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onRefresh() {
                //requestStoreGoodsListRefresh();
//				new requestStoreGoodsListRefresh2().start();
                refreshData();
            }
        });

        lv.setOnLoadListener(new OnLoadListener() {

            @Override
            public void onLoad() {
//				new requestStoreGoodsList2().start();
                reloadData();
            }
        });
    }

    public void reloadData() {
        //全部
        if (currentShows == 1) {
            netRequest.requestGoodsSearch(1, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "",null, null, null, null);
        }
        //促销
        else if (currentShows == 2) {
            netRequest.requestGoodsSearch(1, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "","Y", null, null, null);
        }
        //网店
        else if (currentShows == 3) {
            netRequest.requestGoodsSearch(1, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "",null, "Y", null, null);
        }
        //快选
        else if (currentShows == 4) {
            netRequest.requestGoodsSearch(1, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "",null, null, "Y", null);
        }
        //数据源
        else if (currentShows == 5) {
            //我要批
            if (currentData == 1) {
                netRequest.requestGoodsSearch(1, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "",null, null, null, "woyaopi");
            }
            //惠易定
            else if (currentData == 2) {
                netRequest.requestGoodsSearch(1, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "",null, null, null, "huiyiding");
            }
            //自定义
            else if(currentData == 3){
                netRequest.requestGoodsSearch(1, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "",null, null, null, "zidingyi");
            }
            //领了么==>淘淘领
            else if (currentData == 4) {
                netRequest.requestGoodsSearch(1, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "",null, null, null, "lingleme");
            }
            //伙拼团==>拼团领
            else if(currentData == 5){
                netRequest.requestGoodsSearch(1, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "",null, null, null, "huopintuan");
            }
        }
    }
    /*
    搜索商品数据
     */
    public void queryGoodsData() {
        lv.onLoadComplete();
        pageNum =1;
        //全部
        if (currentShows == 1) {
            netRequest.requestGoodsSearch(3, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "", null, null, null, null);
        }
        //促销
        else if (currentShows == 2) {
            netRequest.requestGoodsSearch(3, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "", "Y", null, null, null);
        }
        //网店
        else if (currentShows == 3) {
            netRequest.requestGoodsSearch(3,pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "", null, "Y", null, null);
        }
        //快选
        else if (currentShows == 4) {
            netRequest.requestGoodsSearch(3, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "", null, null, "Y", null);
        }
        //数据源
        else if (currentShows == 5) {
            //我要批
            if (currentData == 1) {
                netRequest.requestGoodsSearch(3, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "", null, null, null, "woyaopi");
            }
            //惠易定
            else if (currentData == 2) {
                netRequest.requestGoodsSearch(3, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "", null, null, null, "huiyiding");
            }
            //自定义
            else if(currentData == 3){
                netRequest.requestGoodsSearch(3,pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "", null, null, null, "zidingyi");
            }
            //领了么==>淘淘领
            else if (currentData == 4) {
                netRequest.requestGoodsSearch(3, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "",null, null, null, "lingleme");
            }
            //伙拼团==>拼团领
            else if(currentData == 5){
                netRequest.requestGoodsSearch(3, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "",null, null, null, "huopintuan");
            }
        }
    }


    public void refreshData() {
        Log.v("zyl","refreshData");
        showProgressDialog("","正在刷新商品数据...");
//        list.clear();
//        mAdapter.notifyDataSetChanged();
        pageNum = 1;
        //全部
        if (currentShows == 1) {
            netRequest.requestGoodsSearch(2, pageNum + "", pageSize + "",etSearch.getText().toString().trim() + "", null, null, null, null);
        }
        //促销
        else if (currentShows == 2) {
            netRequest.requestGoodsSearch(2, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "","Y", null, null, null);
        }
        //网店
        else if (currentShows == 3) {
            netRequest.requestGoodsSearch(2, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "",null, "Y", null, null);
        }
        //快选
        else if (currentShows == 4) {
            netRequest.requestGoodsSearch(2, pageNum + "", pageSize + "",etSearch.getText().toString().trim() + "", null, null, "Y", null);
        }
        //数据源
        else if (currentShows == 5) {
            //我要批
            if (currentData == 1) {
                netRequest.requestGoodsSearch(2, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "",null, null, null, "woyaopi");
            }
            //惠易定
            else if (currentData == 2) {
                netRequest.requestGoodsSearch(2, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "",null, null, null, "huiyiding");
            }
            //自定义
            else if(currentData == 3){
                netRequest.requestGoodsSearch(2, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "",null, null, null, "zidingyi");
            }

            //领了么==>淘淘领
            else if (currentData == 4) {
                netRequest.requestGoodsSearch(2, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "",null, null, null, "lingleme");
            }
            //伙拼团==>拼团领
            else if(currentData == 5){
                netRequest.requestGoodsSearch(2, pageNum + "", pageSize + "", etSearch.getText().toString().trim() + "",null, null, null, "huopintuan");
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.retreat:
                finish();
                break;
            case R.id.iv_scan:
                Log.v("zyl", "iv_scan");
                //Intent sacnIntent = new Intent(GoodsActivity.this, GoodsCaptureActivity.class);
                Intent sacnIntent = new Intent(GoodsActivity.this, GoodsCaptureActivity.class);
                startActivityForResult(sacnIntent, REQUEST_SCAN);
                break;
            case R.id.iv_add:
                Log.v("zyl", "add");
                Intent addIntent = new Intent(GoodsActivity.this, AddGoodNewActivity.class);
                startActivityForResult(addIntent, REQUEST_ADD);
                break;
            case R.id.ll_all_goods:
                if (currentShows == 1) {
                    return;
                }
                currentShows = 1;
                refreshUI();
                refreshData();
                break;
            case R.id.ll_cuxiao_goods:
                if (currentShows == 2) {
                    return;
                }
                currentShows = 2;
                refreshUI();
                refreshData();
                break;
            case R.id.ll_wangdian_goods:
                if (currentShows == 3) {
                    return;
                }
                currentShows = 3;
                refreshUI();
                refreshData();
                break;
            case R.id.ll_quick_goods:
                if (currentShows == 4) {
                    return;
                }
                currentShows = 4;
                refreshUI();
                refreshData();
                break;
            case R.id.ll_data_source_goods:
                currentShows = 5;
                refreshUI();
                refreshData();
                break;
            case R.id.rl_sjx:
                currentShows = 5;
                refreshUI();
//                popupWindow.showAsDropDown(tv_sjx, 25, 10, Gravity.BOTTOM);
                popupWindow.showAsDropDown(tv_sjx);
                refreshPopUI();
                break;
            default:
                break;
        }
    }

    /**
     * 刷新POP文本UI
     */
    private void refreshPopUI() {
        if (popupWindow != null) {
            if (currentData == 1) {
                tv_wyp.setTextColor(Color.parseColor("#f46534"));
                tv_hyd.setTextColor(Color.parseColor("#333333"));
                tv_zdy.setTextColor(Color.parseColor("#333333"));
                tv_llm.setTextColor(Color.parseColor("#333333"));
                tv_hpt.setTextColor(Color.parseColor("#333333"));
            } else if (currentData == 2) {
                tv_wyp.setTextColor(Color.parseColor("#333333"));
                tv_hyd.setTextColor(Color.parseColor("#f46534"));
                tv_zdy.setTextColor(Color.parseColor("#333333"));
                tv_llm.setTextColor(Color.parseColor("#333333"));
                tv_hpt.setTextColor(Color.parseColor("#333333"));
            }else if(currentData == 3){
                tv_wyp.setTextColor(Color.parseColor("#333333"));
                tv_hyd.setTextColor(Color.parseColor("#333333"));
                tv_zdy.setTextColor(Color.parseColor("#f46534"));
                tv_llm.setTextColor(Color.parseColor("#333333"));
                tv_hpt.setTextColor(Color.parseColor("#333333"));
            }else if(currentData == 4){
                tv_wyp.setTextColor(Color.parseColor("#333333"));
                tv_hyd.setTextColor(Color.parseColor("#333333"));
                tv_zdy.setTextColor(Color.parseColor("#333333"));
                tv_llm.setTextColor(Color.parseColor("#f46534"));
                tv_hpt.setTextColor(Color.parseColor("#333333"));
            }else if(currentData == 5){
                tv_wyp.setTextColor(Color.parseColor("#333333"));
                tv_hyd.setTextColor(Color.parseColor("#333333"));
                tv_zdy.setTextColor(Color.parseColor("#333333"));
                tv_llm.setTextColor(Color.parseColor("#333333"));
                tv_hpt.setTextColor(Color.parseColor("#f46534"));
            }
        }
    }

    public void refreshUI() {
        //全部
        if (currentShows == 1) {
            tv_all_goods.setTextColor(Color.parseColor("#f46534"));
            tv_cuxiao_goods.setTextColor(Color.parseColor("#666666"));
            tv_wangdian_goods.setTextColor(Color.parseColor("#666666"));
            tv_quick_goods.setTextColor(Color.parseColor("#666666"));
            tv_data_source_goods.setTextColor(Color.parseColor("#666666"));

            tv_all_goods.getPaint().setFakeBoldText(true);
            tv_cuxiao_goods.getPaint().setFakeBoldText(false);
            tv_wangdian_goods.getPaint().setFakeBoldText(false);
            tv_quick_goods.getPaint().setFakeBoldText(false);
            tv_data_source_goods.getPaint().setFakeBoldText(false);
        }
        //促销
        else if (currentShows == 2) {

            tv_all_goods.setTextColor(Color.parseColor("#666666"));
            tv_cuxiao_goods.setTextColor(Color.parseColor("#f46534"));
            tv_wangdian_goods.setTextColor(Color.parseColor("#666666"));
            tv_quick_goods.setTextColor(Color.parseColor("#666666"));
            tv_data_source_goods.setTextColor(Color.parseColor("#666666"));

            tv_all_goods.getPaint().setFakeBoldText(false);
            tv_cuxiao_goods.getPaint().setFakeBoldText(true);
            tv_wangdian_goods.getPaint().setFakeBoldText(false);
            tv_quick_goods.getPaint().setFakeBoldText(false);
            tv_data_source_goods.getPaint().setFakeBoldText(false);

        }
        //网店
        else if (currentShows == 3) {
            tv_all_goods.setTextColor(Color.parseColor("#666666"));
            tv_cuxiao_goods.setTextColor(Color.parseColor("#666666"));
            tv_wangdian_goods.setTextColor(Color.parseColor("#f46534"));
            tv_quick_goods.setTextColor(Color.parseColor("#666666"));
            tv_data_source_goods.setTextColor(Color.parseColor("#666666"));

            tv_all_goods.getPaint().setFakeBoldText(false);
            tv_cuxiao_goods.getPaint().setFakeBoldText(false);
            tv_wangdian_goods.getPaint().setFakeBoldText(true);
            tv_quick_goods.getPaint().setFakeBoldText(false);
            tv_data_source_goods.getPaint().setFakeBoldText(false);
        }
        //快选
        else if (currentShows == 4) {
            tv_all_goods.setTextColor(Color.parseColor("#666666"));
            tv_cuxiao_goods.setTextColor(Color.parseColor("#666666"));
            tv_wangdian_goods.setTextColor(Color.parseColor("#666666"));
            tv_quick_goods.setTextColor(Color.parseColor("#f46534"));
            tv_data_source_goods.setTextColor(Color.parseColor("#666666"));

            tv_all_goods.getPaint().setFakeBoldText(false);
            tv_cuxiao_goods.getPaint().setFakeBoldText(false);
            tv_wangdian_goods.getPaint().setFakeBoldText(false);
            tv_quick_goods.getPaint().setFakeBoldText(true);
            tv_data_source_goods.getPaint().setFakeBoldText(false);

        }
        //商品来源
        else if (currentShows == 5) {
            tv_all_goods.setTextColor(Color.parseColor("#666666"));
            tv_cuxiao_goods.setTextColor(Color.parseColor("#666666"));
            tv_wangdian_goods.setTextColor(Color.parseColor("#666666"));
            tv_quick_goods.setTextColor(Color.parseColor("#666666"));
            tv_data_source_goods.setTextColor(Color.parseColor("#f46534"));

            tv_all_goods.getPaint().setFakeBoldText(false);
            tv_cuxiao_goods.getPaint().setFakeBoldText(false);
            tv_wangdian_goods.getPaint().setFakeBoldText(false);
            tv_quick_goods.getPaint().setFakeBoldText(false);
            tv_data_source_goods.getPaint().setFakeBoldText(true);
        }
    }

    public void onEvent(EventBusBean eventBusBean){
        if(eventBusBean.type == 229){
            lv.onRefresh();
        }
    }

    public class requestDelete2 extends Thread {
        private int position;

        public requestDelete2(int position) {
            this.position = position;
        }

        public void run() {
            try {
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                JSONObject paramsObj = new JSONObject();
                try {
                    paramsObj.put("stores_goods_id", list.get(position).getStores_goods_id());
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                params.add(new BasicNameValuePair("params", paramsObj.toString()));
                params.add(new BasicNameValuePair("service", "storesGoods.stores.goodsDelete"));
                params.add(new BasicNameValuePair("user_token", MainActivity.user_token));
                String sign = Submit.genPackageSignUser(params);
                params.add(new BasicNameValuePair("sign", sign));
                JSONObject obj = Submit.submit(params, Stores.http + "/openapi/goods");
                String result = obj.getString("rsp_code");
                if (result.equals("succ")) {
                    Gson gson = new Gson();
                    Message msg = Message.obtain();
                    msg.what = MSG_DELETE_SUCCESS;
                    msg.obj = gson.fromJson(obj.getString("data"), GoodsBean.class);
                    list.remove(position);
                    mHandler.sendMessage(msg);
                } else {
                    String errorMsg = "删除失败";
                    Message msg = Message.obtain();
                    msg.what = MSG_DELETE_FAILED;
                    msg.obj = errorMsg;
                    mHandler.sendMessage(msg);
                }
            } catch (Exception e) {
                String errorMsg = "删除失败";
                Message msg = Message.obtain();
                msg.what = MSG_DELETE_FAILED;
                msg.obj = errorMsg;
                mHandler.sendMessage(msg);
            }
        }
    }


}
